package com.pagepub.expo.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import com.pagepub.expo.entity.CorpCatalog;
import com.pagepub.expo.entity.IdName;

@Repository
public class CorpCatalogDao {
	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate template;
	
	@Autowired
	@Qualifier("appSeqDao")
	private AppSeqDao appSeqDao;
	
	
	private static String tableName="Corp_Catalog";
	
	private static String ID_NAME="SELECT id, catalogName name FROM "+tableName;
	
	private static String QUERY_ALL = "SELECT id, catalogName,catalogDesc,sequence,addTime,updateTime,parentId,catalogLevel FROM "+tableName;

	private static String QUERY_ALL_COUNT = "SELECT COUNT(*) FROM "+  tableName;
	
	private static String INSERT_SQL = "INSERT  INTO "+tableName+" (" +
			"catalogName,catalogDesc,sequence,addTime,updateTime,parentId,catalogLevel)VALUES(" +
			":catalogName,:catalogDesc,:sequence,:addTime,:updateTime,:parentId,:catalogLevel)";
	
	private static String INSERT_SQL_ID = "INSERT  INTO "+tableName+" (" +
	"id,catalogName,catalogDesc,sequence,addTime,updateTime,parentId,catalogLevel)VALUES(" +
	":id,:catalogName,:catalogDesc,:sequence,:addTime,:updateTime,:parentId,:catalogLevel)";

	private static String DELETE_SQL = "DELETE FROM "+ tableName +" WHERE id=?";

	private static String UPDATE_SQL = "UPDATE "+ tableName + " SET " +
			"catalogName=:catalogName,catalogDesc=:catalogDesc,sequence=:sequence," +
			"updateTime=:updateTime,parentId=:parentId,catalogLevel=:catalogLevel"+
			" WHERE id=:id";

	private final ParameterizedRowMapper<CorpCatalog> mapper = new ParameterizedRowMapper<CorpCatalog>() {
		public CorpCatalog mapRow(ResultSet rs, int rowNum) throws SQLException {
			CorpCatalog obj = new CorpCatalog();
			obj.setId(rs.getLong("id"));
			obj.setCatalogName(rs.getString("catalogName"));
			obj.setCatalogDesc(rs.getString("catalogDesc"));
			obj.setAddTime(rs.getTimestamp("addTime"));
			obj.setUpdateTime(rs.getTimestamp("updateTime"));
			obj.setParentId(rs.getString("parentId"));
			obj.setCatalogLevel(rs.getInt("catalogLevel"));
			return obj;
		}
	};

	public List<CorpCatalog> findAll() {
		return template.query(QUERY_ALL, mapper);
	}

	public CorpCatalog findById(Long id) {
		List<CorpCatalog> list = template.query(QUERY_ALL + " WHERE id=?",
				new Object[] { id }, mapper);
		if (list.size() == 0) {
			return null;
		} else
			return list.get(0);
	}
	
	

	
	public void insert(CorpCatalog bean) {
		if(bean.getId()==null){
			bean.setId(new Long(appSeqDao.getSequence("corp_catalog_seq")));
		}		
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(INSERT_SQL_ID, ps);

	}

	public void delete(Long id) {
		template.update(DELETE_SQL, id);
	}

	public void update(CorpCatalog bean) {
		SqlParameterSource ps = new BeanPropertySqlParameterSource(bean);
		NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(
				template.getDataSource());
		npTemplate.update(UPDATE_SQL, ps);
	}
	
	public int getCount(Map<String, String> param){
		int count = template.queryForInt(QUERY_ALL_COUNT);
		return count;
	}
	
	public List findByPage(int currentPage,int pageSize,Map<String, String> param){
		List lists = template.query(QUERY_ALL + " limit ?,?",
				new Object[] { currentPage, pageSize}, mapper);
		return lists;
		
	}
	
	public List<CorpCatalog> getCorpCatalogByParentId(String parentId){
		List<CorpCatalog> list = template.query(QUERY_ALL + " WHERE parentId=?",
				new Object[] { parentId }, mapper);
		return list;
	}
	
	public List<IdName> findIdNames(){
		List lists = template.query(ID_NAME, IdName.idNameMapper);
		return lists;
	}
}
